package es.nadalgoyo.frikiquest.properties;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import es.nadalgoyo.frikiquest.log.Log;
import es.nadalgoyo.frikiquest.util.Util;
import es.nadalgoyo.frikiquest.vo.VersionVO;

public class Properties {
	private Context context;

	private java.util.Properties prop;

	private static final String VERSION_KEY = "version";

	private static final String PATH_KEY = "path.excel";

	private static final String FILE_KEY = "nombre.fichero.excel";

	private static final String VERSION_EXCEL_KEY = "version.excel";

	private static final String IMAGENES_DIR_KEY = "path.imagenes";

	public static final String VERSION_FILE = "version.properties";

	public static final String EXCEL_FILE = "excel.properties";

	public Properties(Context context) {
		this.context = context;
		try {
			if (!getLocalProperties().exists()
					|| context.openFileInput(VERSION_FILE).available() == -1) {

				InputStream in = context.getAssets().open(VERSION_FILE);
				FileOutputStream out = new FileOutputStream(
						getLocalProperties());

				Util.copyFile(in, out);

				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			}
		} catch (Exception e) {
			Log.e(e.getMessage());
		}

		prop = new java.util.Properties();
	}

	private java.util.Properties getVersionProp() throws IOException {
		InputStream is = getVersionIS();
		prop.load(is);
		is.close();
		return prop;
	}

	public InputStream getVersionIS() throws FileNotFoundException {
		return context.openFileInput(VERSION_FILE);
	}

	public VersionVO getVersion() throws IOException {
		return Util.getVersion(getVersionProp().getProperty(VERSION_KEY));
	}

	public void setVersion(VersionVO version) throws IOException {
		prop = getVersionProp();
		prop.setProperty(VERSION_KEY,
				version.getVersion().concat(".")
						.concat(version.getSubVersion()));

		prop.store(new FileOutputStream(getLocalProperties()), null);
	}

	private java.util.Properties getExcelProp() throws IOException {
		InputStream is = getExcelIS();
		prop.load(is);
		is.close();
		return prop;
	}

	public InputStream getExcelIS() throws FileNotFoundException {
		return context.openFileInput(EXCEL_FILE);
	}

	public String getExcelName() throws IOException {
		return getExcelProp().getProperty(FILE_KEY);
	}

	public String getExcelPath() throws IOException {
		return getExcelProp().getProperty(PATH_KEY);
	}

	public VersionVO getExcelVersion() throws IOException {
		return Util.getVersion(getExcelProp().getProperty(VERSION_EXCEL_KEY));
	}

	public String getExcelImagenesDir() throws IOException {
		return getExcelProp().getProperty(IMAGENES_DIR_KEY);
	}

	private File getLocalProperties() {
		return new File(context.getApplicationContext().getFilesDir()
				.getAbsolutePath().concat(File.separator).concat(VERSION_FILE));
	}

}
